package parser;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class CreateTestDocuments {
   
   static int numberOfTest = 0;
   static int numberOfTypes = 0;
   static int numberOfTestDocuments = 0;
   static int numberOfTrainingDocs = 0;
   static ArrayList<String> topics = new ArrayList<String>();
   static ArrayList<Integer> numberOfTopics = new ArrayList<Integer>();
   static Logger LOGGER = Logger.getLogger("CreateTestDocumentsLogger"); 
   
   public static void main(String[] args) throws IOException {
      
      RouterParser parser = new RouterParser();
      parser.parser("Routers");
      FileHandler fileHander = new FileHandler("CreateTestDocumentsLogger.log");
      LOGGER.addHandler(fileHander);
      SimpleFormatter formatter = new SimpleFormatter();
      fileHander.setFormatter(formatter);
      
      System.setIn(new FileInputStream("runConfigs/config"));
      Scanner scanner = new Scanner(System.in);
     
      for (int i = 4; i < 500; i++) {
         for (int j = 3; j <= i/2; j++) {
               if(i % j == 0) {
               System.out.println("Enter Number of Test Cases To Make");
               numberOfTest = i < 50 ? 100 : 10000;//scanner.nextInt();
               System.out.println("Enter Number of Types");
               numberOfTypes = j;//scanner.nextInt();
               System.out.println("Enter Number of Test Documents To Be Included");
               numberOfTestDocuments = (int) Math.ceil((i * 2/3.0));//scanner.nextInt();
               System.out.println("Enter Number of Training Documents");
               numberOfTrainingDocs = (int) Math.ceil((i * 1/3.0)); //scanner.nextInt();
               
               setTopics(parser);
               
               createdDocs(parser);
            }
         }
      }
      
   }
   
   private static void getUserInput() {
      // TODO Auto-generated method stub
      
   }
   
   private static void setTopics(RouterParser parser) {
      for (String topic : parser.topicToTrainDocument.keySet()) {
         
         if (parser.topicToTrainDocument.get(topic) != null && parser.topicToTestDocument.get(topic) != null &&
               parser.topicToTrainDocument.get(topic).size() >= numberOfTrainingDocs &&
               parser.topicToTestDocument.get(topic).size() >= numberOfTestDocuments) {
            topics.add(topic);
         }
      }
      
   }
   
   private static boolean createdDocs(RouterParser parser) throws IOException {
      if (topics.size() < numberOfTypes) {
         System.out.println("Not Enough Types");
         return false;
      }
      
      for (int i = 0; i < topics.size(); i++) {
         numberOfTopics.add(i);
      }
      
      for (int i = 0; i < numberOfTest; i++) {
         String title = "";
         ArrayList<String> documentToRightToFile = new ArrayList<String>();
         Collections.shuffle(numberOfTopics);
         double startTime = System.nanoTime();
         for (int j = 0; j < numberOfTypes; j++) {
            String curTopic = topics.get(numberOfTopics.get(j));
            title = title + curTopic + "_";
            ArrayList<String> documentsForTopic = parser.topicToTrainDocument.get(curTopic);
            Collections.shuffle(documentsForTopic);
            
            if(documentsForTopic.size() < numberOfTrainingDocs) {
               return false;
            }
            for (int k = 0; k < numberOfTrainingDocs; k++) {
               documentToRightToFile.add(documentsForTopic.get(k));
            }
            
            documentsForTopic = parser.topicToTestDocument.get(curTopic);
            
            if(documentsForTopic.size() < numberOfTestDocuments) {
               return false;
            }
            
            for (int k = 0; k < numberOfTestDocuments; k++) {
               documentToRightToFile.add(documentsForTopic.get(k));
            }
         }
         
         title += "TYPES_"+ numberOfTypes +"_TRAINING_" + numberOfTrainingDocs + "_TEST_"
               + numberOfTestDocuments + "_TOTAL_" + numberOfTypes * (numberOfTestDocuments + numberOfTrainingDocs);
         title = numberOfTypes * (numberOfTestDocuments + numberOfTrainingDocs) + "_" + title;
         if((numberOfTrainingDocs + numberOfTestDocuments) * numberOfTypes == documentToRightToFile.size()) {
            double endTime = System.nanoTime();
            LOGGER.log(Level.INFO, documentToRightToFile.size()+", " + numberOfTypes + ", "+ numberOfTrainingDocs + ", " + numberOfTestDocuments + ", " + (endTime - startTime) + "\n");
            writeToFile(documentToRightToFile, title);
         }
         
      }
      return true;
   }
   
   private static void writeToFile(ArrayList<String> documentToRightToFile, String title) throws IOException {
      Writer output = null;
      File file = new File("documentsForTesting/" + title);
      output = new BufferedWriter(new FileWriter(file));
      
      for (int i = 0; i < (numberOfTestDocuments + numberOfTrainingDocs); i++) {
         try {
            int k = i;
            
            for (int j = 0; j < numberOfTypes; j++) {
               output.write(documentToRightToFile.get(k) + "\n");
               k+= (numberOfTestDocuments + numberOfTrainingDocs);
            }
            
         }
         catch (Exception e) {
            System.out.println("Could not create file");
         }
      }
      
      output.close();
      System.out.println("WROTE TO FILE: " + title);
   }
}
